/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import pasremote.PASRemote;
import entities.Assignment;
import entities.PASSUser;
import entities.Submission;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateful;

// TODO:
// acount check
/**
 *
 * @author ryuju
 */
@Stateful
public class PAS implements PASRemote {

    @EJB
    private UserManager userManager;
    @EJB
    private AssignmentManager assignmentManager;
    @EJB
    private SubmissionManager submissionManager;
    PASSUser loginUser = null; // for permission check.

    @Override
    public boolean login(String id, String pw) {
        try {
            PASSUser user = userManager.login(id, pw);
            if (user == null) {
                return false;
            }
            loginUser = user;
        } catch (Exception e) {
            // just return false.
            Logger.getLogger(PAS.class.getName()).log(Level.INFO, "login fail " + id);
            return false;
        }
        return true;
    }

    @Override
    public boolean register(String id, String pw, String userType, String asst_pw) {
        Logger.getLogger(PAS.class.getName()).log(Level.INFO, "register " + id);
        try {
            return userManager.register(id, pw, userType, asst_pw);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public String getUserType() {
        try {
            return loginUser.getUserType();
        } catch (Exception e) {
            return "";
        }
    }

    @Override
    public String getLoginUserName() {
        try {
            return loginUser.getLoginName();
        } catch (Exception e) {
            return "Can't get login id";
        }
    }

    @Override
    public Assignment getAssignment(Long asmtID) {
        try {
            return assignmentManager.getAssignment(asmtID);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<Assignment> getAssignments() {
        try {
            return assignmentManager.getAssignments();
        } catch (Exception e) {
            return new ArrayList<Assignment>();
        }
    }

    @Override
    public List<Assignment> getAllAssignments() {
        try {
            return assignmentManager.getAllAssignments();
        } catch (Exception e) {
            return new ArrayList<Assignment>();
        }
    }

    @Override
    public boolean createAssignment(Assignment assignment) {
        if (null == assignment) {
            return false;
        }
        try {
            return assignmentManager.createAssignment(assignment);
        } catch (Exception e) {
            // something failure..
            // just return false.
            // handle later.
            return false;
        }
    }

    @Override
    public boolean modifyAssignment(Assignment assignment) {
        if (null == assignment) {
            return false;
        }

        try {
            return assignmentManager.modifyAssignment(assignment);
        } catch (Exception e) {
            // something failure..
            // just return false.
            // handle later.
            return false;
        }
    }

    @Override
    public boolean submit(Submission submission) {
        if (null == submission) {
            return false;
        }
        try {
            return submissionManager.submit(submission);
        } catch (Exception e) {
            // something failure..
            // just return false.
            // handle later.
            return false;
        }
    }

    @Override
    public boolean rejudge(Long asmtID) {
        if (null == asmtID) {
            return false;
        }

        try {
            return submissionManager.rejudge(asmtID);
        } catch (Exception e) {
            // something failure..
            // just return false.
            // handle later.
            return false;
        }
    }

    @Override
    public List<Submission> getAllSubmissions() {
        try {
            return submissionManager.getAllSubmissions();
        } catch (Exception e) {
            // return empty list
            return new ArrayList<Submission>();
        }
    }

    @Override
    public List<Submission> getSubmissionsByAssignmentID(Long asmtID) {
        if (null == asmtID) {
            return null;
        }
        try {
            return submissionManager.getSubmissionsByAssignmentID(asmtID);
        } catch (Exception e) {
            // return empty list
            return new ArrayList<Submission>();
        }
    }

    @Override
    public List<Submission> getSubmissionsByUserLoginName(String name) {
        if (null == name || name.trim().length() == 0) {
            return null;
        }

        try {
            return submissionManager.getSubmissionsByUserLoginName(name);
        } catch (Exception e) {
            // return empty list
            return new ArrayList<Submission>();
        }
    }
}
